#include "dialog_configsystermparas.h"
#include "ui_dialog_configsystermparas.h"

Dialog_configSystermParas::Dialog_configSystermParas(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_configSystermParas)
{
    ui->setupUi(this);
    readIni();

}

Dialog_configSystermParas::~Dialog_configSystermParas()
{
    delete ui;
}

void Dialog_configSystermParas::on_btn_save_clicked()
{
    writeIni();
    accept();
}


void Dialog_configSystermParas::on_btn_cancel_clicked()
{
    reject();
}

QString Dialog_configSystermParas::removePrefix(const QString &str, const QString &prefix) {  
    if (str.startsWith(prefix)) {  
        return str.mid(prefix.length()); // 去除前缀，返回剩余部分  
    }  
    return str; // 如果没有前缀，则返回原字符串  
}

void Dialog_configSystermParas::writeIni() {
    QSettings settings("config/systemConfig.ini", QSettings::IniFormat);
    settings.beginGroup("system");
    settings.setValue("PlcInterfacePath", ui->LineEdit_PlcInterfacePath->text());
    settings.setValue("netIP", ui->LineEdit_netIP->text());
    settings.setValue("netPort", ui->LineEdit_netPort->text());
    settings.setValue("configFilePath", ui->LineEdit_configFilePath->text());
    settings.setValue("topCameraFocusRingNuberOf1", ui->LineEdit_topCameraFocusRingNuberOf1->text());
    settings.setValue("topCameraLightNuberOf1", ui->LineEdit_topCameraLightNuberOf1->text());
    settings.setValue("LineEdit_sideCameraFocusRingNuberOf1", ui->LineEdit_sideCameraFocusRingNuberOf1->text());
    settings.setValue("LineEdit_sideCameraLightNuberOf1", ui->LineEdit_sideCameraLightNuberOf1->text());
    settings.setValue("bottomCameraLightNuberOf2", ui->LineEdit_bottomCameraLightNuberOf2->text());
    settings.endGroup();
}

void Dialog_configSystermParas::readIni() {
    QSettings settings("config/systemConfig.ini", QSettings::IniFormat);
    settings.beginGroup("system");
    ui->LineEdit_PlcInterfacePath->setText(removePrefix(settings.value("PlcInterfacePath").toString(), m_prefix));
    ui->LineEdit_netIP->setText(settings.value("netIP").toString());
    ui->LineEdit_netPort->setText(settings.value("netPort").toString());
    ui->LineEdit_configFilePath->setText(settings.value("configFilePath").toString());
    ui->LineEdit_topCameraFocusRingNuberOf1->setText(removePrefix(settings.value("topCameraFocusRingNuberOf1").toString(), m_prefix));
    ui->LineEdit_topCameraLightNuberOf1->setText(removePrefix(settings.value("topCameraLightNuberOf1").toString(), m_prefix));
    ui->LineEdit_sideCameraFocusRingNuberOf1->setText(removePrefix(settings.value("LineEdit_sideCameraFocusRingNuberOf1").toString(), m_prefix));
    ui->LineEdit_sideCameraLightNuberOf1->setText(removePrefix(settings.value("LineEdit_sideCameraLightNuberOf1").toString(), m_prefix));
    ui->LineEdit_bottomCameraLightNuberOf2->setText(removePrefix(settings.value("bottomCameraLightNuberOf2").toString(), m_prefix));
    settings.endGroup();
}
